import numpy as np


# 1106 借鉴csdn
def cross(p1, p2, p3):  # 叉积判定
    x1 = p2[0]-p1[0]
    y1 = p2[1]-p1[1]
    x2 = p3[0]-p1[0]
    y2 = p3[1]-p1[1]
    return x1*y2-x2*y1


def segment(p1, p2, p3, p4):  # 判断两线段是否相交
    # 矩形判定，以l1、l2为对角线的矩形必相交，否则两线段不相交
    df = 0
    if(max(p1[0], p2[0]) >= min(p3[0], p4[0])  # 矩形1最右端大于矩形2最左端
            and max(p3[0], p4[0]) >= min(p1[0], p2[0])  # 矩形2最右端大于矩形1最左端
            and max(p1[1], p2[1]) >= min(p3[1], p4[1])  # 矩形1最高端大于矩形2最低端
            and max(p3[1], p4[1]) >= min(p1[1], p2[1])):  # 矩形2最高端大于矩形1最低端
        if(cross(p1, p2, p3)*cross(p1, p2, p4) <= 0
                and cross(p3, p4, p1)*cross(p3, p4, p2) <= 0):
            df = 1
    return df


def check(l1, l2, sq_min, sq_max):  # [x_leftdown, y_leftdown, x_rightup, y_rightup]
    # step 1 check if end point is in the square
    sq = [sq_min[0], sq_min[1], sq_max[0], sq_max[1]]
    if (sq[0] <= l1[0] <= sq[2] and sq[1] <= l1[1] <= sq[3]) or sq[0] <= l2[0] <= sq[2] and sq[1] <= l2[1] <= sq[3]:
        return 1
    else:
        # step 2 check if diagonal cross the segment
        p1 = [sq[0], sq[1]]
        p2 = [sq[2], sq[3]]
        p3 = [sq[2], sq[1]]
        p4 = [sq[0], sq[3]]
        if segment(l1,l2,p1,p2) or segment(l1,l2,p3,p4):
            return 1
        else:
            return 0  # 不相交

# | Xb2+Xb1-Xa2-Xa1 | <= Xa2-Xa1 + Xb2-Xb1
# | Yb2+Yb1-Ya2-Ya1 | <= Ya2-Ya1 + Yb2-Yb1
def cross_rec(beginPoint, endPoint, object_min, object_max):
    w_left = abs(beginPoint[0]+endPoint[0]-object_min[0]-object_max[0])
    w_right = abs(beginPoint[0]-endPoint[0]+object_min[0]-object_max[0])
    h_left = abs(beginPoint[1]+endPoint[1]-object_min[1]-object_max[1])
    h_right = abs(beginPoint[1]-endPoint[1]+object_min[1]-object_max[1])
    if w_left <= w_right and h_left <= h_right:
        return True


def intersection_rect_poly(poly_points, rect_x, rect_y):
    oddNodes = False
    if not poly_points:
        return oddNodes
    # 去除表示范围的多边形里有完全一样的点
    pa = None
    for i, p in enumerate(poly_points):
        if pa == p:
            # print('==', i, p)
            poly_points.remove(p)
        else:
            pa = p
    # 多边形polygon的控制矩形坐标值
    poly_x_max, poly_x_min, y_temp = max(poly_points)[0], min(poly_points)[0], [i[1] for i in poly_points]
    poly_y_max, poly_y_min = max(y_temp), min(y_temp)
    # 矩形rectangle坐标值
    rect_x_max, rect_x_min = rect_x
    rect_y_max, rect_y_min = rect_y
    rect_points = [[rect_x_min, rect_y_min], [rect_x_max, rect_y_min], [rect_x_min, rect_y_max], [rect_x_max, rect_y_max]]

    # 判断矩形与多边形控制矩形是否相交
    if (poly_x_min < rect_x_max < poly_x_max or poly_x_min < rect_x_min < poly_x_max) and (poly_y_min < rect_y_min < poly_y_max or poly_y_min < rect_y_max < poly_y_max):
        # 判断矩形顶点与多边形边的关系
        for point in rect_points:
            x, y = point[0], point[1]
            j = len(poly_points) - 1
            for i in range(len(poly_points)):
                if poly_points[i][1] < y < poly_points[j][1] or poly_points[j][1] < y < poly_points[i][1] and x >= poly_points[i][0] or x >= poly_points[j][0]:
                    if (poly_points[j][1] - poly_points[i][1]) != 0:
                        if (poly_points[i][0] + (y - poly_points[i][1]) / (poly_points[j][1] - poly_points[i][1]) * (poly_points[j][0] - poly_points[i][0])) < x:
                            oddNodes = True
                j = i
    return oddNodes


if __name__ == '__main__':
    beginPoint = np.array((0,1))
    endPoint = np.array((2,1))
    p1 = np.array((3,0))
    p2 = np.array((3,2))
    # if verify_line(beginPoint,endPoint,p1,p2):
    #     print('两条线香蕉')
    # else:
    #     print('两条线不香蕉')




